from gmoney.members.models import Team, Member, Charging, Activity

def getMembers(team_id):
    team = Team.objects.get(id = team_id)
    return team.members.all()

def addMember(team_id, nickname):
    team = Team.objects.get(id = team_id)
    member = Member(nickname = nickname)
    member.save()
    team.members.add(member)
    
def addCharging(team_id, member_id, money):
    
    team = Team.objects.get(id = team_id)
    member = Member.objects.get(id = member_id)
    # begin transition
    charging = Charging.objects.create(member = member,money = money)
    member.income += money
    member.balance += money
    member.save()
    team.income += money
    team.balance += money
    team.save()
    #commit

def getActivities(team_id):
    team = Team.objects.get(id = team_id)
    return team.activity_set.all()

def addActivity(team_id, timestamp, place, content, money, members):
    team = Team.objects.get(id = team_id)
    activity = Activity.objects.create(timestamp = timestamp, place = place, content = content, money=money, team = team)
    expense = money / len(members)
    for member in members:
        #member = Member.objects.get(id = member_id)
        activity.members.add(member)
        member.outcome -= expense
        member.balance -= expense
        member.save()
    team.outcome -= expense
    team.balance -= expense
    team.save()
    
